﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YJKDataBase;

namespace YJKArchiFormat
{
	public partial class DlgEditCategory : System.Windows.Forms.Form
	{
		public DlgEditCategory(IDlgBase main)
		{
			this.mForm = main;
			this.InitializeComponent();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				this.mForm.CurrentCategory.Database.CategoryTable.Update(this.mForm.CurrentCategory);
				DbCategory dbCategory = (DbCategory)this.cbxCat.SelectedItem;
				this.mForm.CurrentSubject.Database.CategoryTable.Update(dbCategory);
				this.mForm.TrCategory.SelectedNode.Text = dbCategory.Name;
				base.Close();
			}
			catch
			{
				MessageBox.Show("更新失败，请检查分类是否重名");
			}
		}

		public static DlgEditCategory GetInstance(IDlgBase main)
		{
			if (DlgEditCategory.dac == null || DlgEditCategory.dac.IsDisposed)
			{
				DlgEditCategory.dac = new DlgEditCategory(main);
			}
			return DlgEditCategory.dac;
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxRecordParam_DrawItem(object sender, DrawItemEventArgs e)
		{
			int index = e.Index;
		}

		private void button1_Click(object sender, EventArgs e)
		{
		}

		private void tbxParamName_TextChanged(object sender, EventArgs e)
		{
			if (new Regex("^\\w").Match(this.tbxParamName.Text).Success)
			{
				this.btnAdd.Enabled = true;
				return;
			}
			this.btnAdd.Enabled = false;
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbxCat_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.cmdRemoveParam();
			}
		}

		private void cmdRemoveParam()
		{
		}

		private void DlgEditCategory_Load(object sender, EventArgs e)
		{
			if (this.mForm.TrCategory.SelectedNode != null && this.mForm.TrCategory.SelectedNode.Tag != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				if (this.mForm.CurrentCategory.ParentId == this.mForm.CurrentSubject.Guid)
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentSubject);
				}
				else
				{
					this.cbxCategory.Items.Add(this.mForm.CurrentCategory.Database.CategoryTable[this.mForm.CurrentCategory.ParentId]);
				}
				this.cbxCategory.SelectedIndex = 0;
				this.cbxCat.Items.Add(this.mForm.CurrentCategory);
				this.cbxCat.SelectedIndex = 0;
			}
			this.lbxRecordParam.DoubleClick += this.lbxRecordParam_DoubleClick;
			this.lbxRecordParam.DrawItem += this.lbxRecordParam_DrawItem;
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.cmdRemoveParam();
			}
		}

		private void propertyGrid2_SelectedGridItemChanged(object sender, SelectedGridItemChangedEventArgs e)
		{
		}

		private static DlgEditCategory dac;

		private IDlgBase mForm;
	}
}
